var amqp = require('amqplib/callback_api');

var args = process.argv.slice(2);
if (args.length == 0) {
	console.log("Usage: receive_logs_topic.js <facility>.<severity>");
	process.exit(1);
}

amqp.connect('amqp://localhost', function(err, conn) {
	conn.createChannel(function(err, ch) {
		var ex = 'topic_logs';
		ch.assertExchange(ex, 'topic', {
			durable: false
		});

		ch.assertQueue('', {
			exclusive: true
		}, function(err, q) {
			// bind queue and exchange
			args.forEach(function(key) {
				ch.bindQueue(q.queue, ex, key);
			});

			ch.consume(q.queue, function(msg) {
				console.log(" [x] %s", msg.content.toString());
			}, {
				noAck: true
			});
		});
	});
});

// node c.js 2
// then
// node c.js -1